package com.situ.mall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mall.common.ServerResponse;
import com.situ.mall.entity.Cart;
import com.situ.mall.mapper.CartMapper;
import com.situ.mall.service.ICartService;
import com.situ.mall.vo.CartItemVO;
import com.situ.mall.vo.CartVO;

@Service
public class CartServiceImpl implements ICartService {
	@Autowired
	private CartMapper cartMapper;
	

	@Override
	public ServerResponse insert(Cart cart) {
		Integer count = cartMapper.insert(cart);
		if (count >= 0) {
			return ServerResponse.createSuccess("添加成功");
		} else {
			return ServerResponse.createError("添加失败");
		}
	}

	//数据库查询购物车订单
	@Override
	public List<Cart> selectByUserId(Integer id) {
		return cartMapper.selectByUserId(id);
	}

	@Override
	public ServerResponse deleteByProductId(Integer productId) {
		try {
			Integer count = cartMapper.deleteByProductId(productId);
			if (count == 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

	@Override
	public ServerResponse update(Cart cart) {
		try {
			Integer count = cartMapper.updateByProductIdSelective(cart);
			if (count == 1) {
				return ServerResponse.createSuccess("修改成功");
			} else {
				return ServerResponse.createError("修改失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("修改失败");
		}
	}

	//生成订单后删除cart表里面check=1的
	@Override
	public void deleteByUserId(Integer id) {
		cartMapper.deleteByUserId(id);
	}

	//添加到购物车查看有没有这个商品
	@Override
	public Integer selectByProductId(Integer productId) {
		return cartMapper.selectByProductId(productId);
	}

}
